$(function () {
    $("#jqGrid").Grid({
        url: '../merchantcoin/list',
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '交易编号', name: 'tradeNo', index: 'trade_no', width: 100},
            {label: '商家编号', name: 'mid', index: 'mid', width: 40},
            {label: '商家用户编号', name: 'uid', index: 'uid', width: 40},
            {label: '商家手机号', name: 'phone', index: 'phone', width: 50},
            {label: '订单数量', name: 'sportCoin', index: 'sport_coin', width: 50},
            {label: '实际数量', name: 'coin', index: 'coin', width: 50},
            {label: '手续费', name: 'rateValue', index: 'rate_value', width: 40},
            {label: '手续费率', name: 'rate', index: 'rate', width: 40},
            {label: '兑换人id', name: 'consumUid', index: 'consum_uid', width: 40},
            {label: '兑换人手机号', name: 'consumPhone', index: 'consum_phone', width: 50},
            {label: '产品编号', name: 'pid', index: 'pid', width: 40},
            {label: '产品名称', name: 'productName', index: 'product_name', width: 80},
            {
                label: '交易类型', name: 'type', index: 'type', width: 80, formatter: function (value) {
                    if (value == 1) {
                        return '商品兑换'
                    }
                    return value;
                }
            },
            {label: '创建时间', name: 'createdDate', index: 'created_date', width: 80}]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        merchantCoin: {},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.merchantCoin = {};
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.merchantCoin.id == null ? "../merchantcoin/save" : "../merchantcoin/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.merchantCoin),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../merchantcoin/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../merchantcoin/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.merchantCoin = r.merchantCoin;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});